﻿using System.Collections.Generic;
using Common.Logging;
using NHibernate;
using Spring.Data.NHibernate;
using Spring.Data.NHibernate.Generic.Support;

namespace Framework.Core.Domain.Dao.SpringNHB
{
    public class SpringNHBFactoryDaoBase<T, IDT> : HibernateDaoSupport, IFactoryDaoBase<T, IDT>
    {
        public SpringNHBFactoryDaoBase()
        {
            Logger = LogManager.GetLogger(ToString());
        }

        #region GenericSpringNHBFactoryDao<T,IDT> 成员

        public SubT GetById<SubT>(IDT entityId, bool shouldLock) where SubT : T
        {
            return HibernateTemplate.Load<SubT>(entityId);
        }

        public IList<SubT> GetAll<SubT>() where SubT : T
        {
            return HibernateTemplate.LoadAll<SubT>();
        }

        public void Save(T entity)
        {
            HibernateTemplate.Save(entity);
        }

        public void Update(T entity)
        {
            HibernateTemplate.Update(entity);
        }

        public void SaveOrUpdate(T entity)
        {
            HibernateTemplate.SaveOrUpdate(entity);
        }

        public void Delete(T entity)
        {
            HibernateTemplate.Delete(entity);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="conditions"></param>
        /// <param name="startIndex"></param>
        /// <param name="count"></param>
        /// <param name="sortInfo"></param>
        /// <returns></returns>
        public IList<SubT> GetByConditions<SubT>(List<string> conditions, int startIndex, int count, SortInfo sortInfo)
            where SubT : T
        {
            string alias = typeof (SubT).Name.ToLower();
            string hsql = string.Format("From {0} as {1} Where 1=1", typeof (SubT), alias);
            if (conditions != null)
            {
                foreach (string condition in conditions)
                {
                    hsql += " " + condition.Trim();
                }
            }
            if (sortInfo != null)
            {
                if (!string.IsNullOrEmpty(sortInfo.Field))
                {
                    hsql += string.Format(" Order By {0}.{1} {2}", alias, sortInfo.Field, sortInfo.Direction);
                }
            }
            IQuery query = SessionFactoryUtils.GetSession(HibernateTemplate.SessionFactory, true).CreateQuery(hsql);
            query.SetFirstResult(startIndex);
            query.SetMaxResults(count);
            IList<SubT> results = query.List<SubT>();
            return results;
        }

        public int GetCountByConditions<SubT>(List<string> conditions) where SubT : T
        {
            string hsql = string.Format("Select count(*) From {0} as {1} Where 1=1 ", typeof (SubT),
                                        typeof (SubT).Name.ToLower());

            if (conditions != null)
            {
                foreach (string condition in conditions)
                {
                    hsql += condition;
                }
            }

            IQuery query = SessionFactoryUtils.GetSession(HibernateTemplate.SessionFactory, true).CreateQuery(hsql);
            object o = query.UniqueResult();
            return o == null ? 0 : int.Parse(o.ToString());
        }

        #endregion

        public ILog Logger { get; set; }
    }
}